package com.F09.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import com.F09.actionform.F0905ActionForm;
import com.dao.DataOperations;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.info.FunctionLanguageInfo;
import com.util.CommonTools;

public class FunctionLanguageDAO extends DataOperations{
	Logger log = Logger.getLogger(FunctionLanguageDAO.class);
	public FunctionLanguageDAO(){
		
	}
    public FunctionLanguageDAO(Connection connection){
        super(connection);
    }
    
    public List queryList(FunctionLanguageInfo info) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();
    	sql.append(" SELECT a.id,a.name,a.FUNCTIONID,a.language,b.name languagename ");
    	sql.append(" FROM T_S_FUNCTION_LANGUAGE a,T_S_FUNCTION_LANGUAGE_CODE b,t_s_function c");
    	sql.append(" WHERE a.language = b.code and a.functionid=c.functionid");
    	if(!info.getLanguage().equals("")){
    		sql.append(" and a.language ='"+info.getLanguage()+"'");
    	}
    	sql.append(" ORDER BY c.code,c.xh ");
    	ResultSet rest = null;
    	try{
    		log.info(sql.toString());
    		rest = this.executeQuery(sql.toString());
    		while(rest.next()){
    			Map map = new HashMap();
    			map.put("ID", CommonTools.null2String(rest.getString("ID")));
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				map.put("NAME", CommonTools.null2String(rest.getString("NAME")));
				map.put("LANGUAGE", CommonTools.null2String(rest.getString("LANGUAGE")));
				map.put("LANGUAGENAME", CommonTools.null2String(rest.getString("LANGUAGENAME")));
				list.add(map);
    		}
    	}catch(SQLException e){
    		e.printStackTrace();
    	}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
    
    public List selectByObject() throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();   
    	//oracle 语句
    	//sql.append("SELECT FUNCTIONID,CODE,NAME,PARENTID,'('||FUN_GETBH('FUNCTIONID',PARENTID)||')'||FUN_GETMC('FUNCTIONID',PARENTID) PARENTNAME, URL, NOTES  FROM T_S_FUNCTION ");
    	//sql.append(" where FUNCTIONID!='0' ORDER BY PARENTID,XH ASC ");
    	sql.append("SELECT a.id,a.name,a.FUNCTIONID,a.language,b.name languagename  FROM T_S_FUNCTION_LANGUAGE a,T_S_FUNCTION_LANGUAGE_CODE b");
    	sql.append(" where a.language = b.code");
    	sql.append(" ORDER BY language,FUNCTIONID ASC");
    	ResultSet rest = null;
    	try {
    		log.info(sql.toString());
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("ID", CommonTools.null2String(rest.getString("ID")));
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				map.put("NAME", CommonTools.null2String(rest.getString("NAME")));
				map.put("LANGUAGE", CommonTools.null2String(rest.getString("LANGUAGE")));
				map.put("LANGUAGENAME", CommonTools.null2String(rest.getString("LANGUAGENAME")));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
    	return list;
    }
    
    //得到语言列表
    public  List getCollectionList(){
		List list=new ArrayList();
		String sql="SELECT code, name FROM t_s_function_language_code ";
		Map map=null;
		ResultSet rest=null;
		try{
			log.info(sql.toString());
			rest=super.executeQuery(sql);
			while(rest.next()){
				map=new HashMap();
				map.put("CO",CommonTools.null2String(rest.getString("code")));
				map.put("NA",CommonTools.null2String(rest.getString("name")));
				list.add(map);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return list;
	}
    public boolean update(FunctionLanguageInfo info,F0905ActionForm form){
    	boolean flag =false;
    	StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("update t_s_function_language set ");
		sql.append(" name='"+info.getName()+"',");
		sql.append(" functionid= "+info.getFunctionId());
		sql.append(" ,language='"+info.getLanguage()+"'");
		sql.append(" where id="+info.getId());
		sqlList.add(sql.toString());
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 	
			form.setErrorFlag("保存成功!!!");
		}catch(Exception e){
	        e.printStackTrace();
	        form.setErrorFlag("保存失败!!!");
		}
		return flag;
    }
    
    public boolean delete(String id,F0905ActionForm form){
		boolean flag=true;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("DELETE FROM t_s_function_language    ");
		sql.append("WHERE id ="+id);
		sqlList.add(sql.toString());
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true);
			form.setErrorFlag("删除成功!!!");
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("删除失败!!!");
		}
		return flag;
	}
    
    public boolean insert(FunctionLanguageInfo info,F0905ActionForm form){
		boolean flag=false;
		StringBuffer sql=new StringBuffer();
		List sqlList = new ArrayList();
		sql.append("INSERT INTO t_s_function_language   ");
		sql.append("  (name, functionid, language) ");
		sql.append("VALUES      ('"+info.getName()+"', "+info.getFunctionId()+",  ");
		sql.append("             '"+info.getLanguage()+"') ");
		sqlList.add(sql.toString());
		try{
			log.info("sql is "+sql.toString());
			flag=super.executeUpdate(sqlList,true); 
			form.setErrorFlag("保存成功!!!");
		}catch(SQLException ex){
			if("1".equals(String.valueOf(ex.getErrorCode()))){
				form.setErrorFlag("主键重复,错误序号: "+String.valueOf(ex.getErrorCode()));
			}else{
				form.setErrorFlag("保存失败!!!");
			}
		}catch(Exception e){
			e.printStackTrace();
			form.setErrorFlag("保存失败!!!");
		}
		return flag;
	}
    
    public Map queryDetail(String id,String language)throws SelectException{
		Map map=null;
		StringBuffer sql=new StringBuffer();
		sql.append("SELECT name, functionid, language           ");
		sql.append("FROM   t_s_function_language    ");
		sql.append("WHERE  functionid="+id+"   ");
		sql.append("AND    language='"+language+"' ");
		ResultSet rest=null;
		try{
			log.info("sql is "+sql);
			rest=super.executeQuery(sql.toString());
			while(rest.next()){
				map=new HashMap();
				map.put("NAME",CommonTools.null2String(rest.getString("name")));
				map.put("FUNCTIONID",CommonTools.null2String(rest.getString("functionid")));
				map.put("LANGUAGE",CommonTools.null2String(rest.getString("language")));
				
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return map;
	}
    
    public List selectById(String id) throws SelectException{
    	List list = new ArrayList();
    	StringBuffer sql = new StringBuffer();    	
    	//sql.append("SELECT FUNCTIONID, CODE, NAME, PARENTID,'(' ||FUN_GETBH('FUNCTIONID',PARENTID)|| ')' || FUN_GETMC('FUNCTIONID',PARENTID) PARENTNAME, URL, NOTES,XH  FROM T_S_FUNCTION ");
    	//sql.append(" where FUNCTIONID = '").append(key).append("'" );
    	
    	sql.append("SELECT A.ID,A.FUNCTIONID,A.NAME, A.LANGUAGE,'('+B.CODE+')'+B.NAME FUNCTIONNAME  FROM T_S_FUNCTION_LANGUAGE A,T_S_FUNCTION B");
    	sql.append(" where A.ID = ").append(id);
    	sql.append(" and A.FUNCTIONID=B.FUNCTIONID" );
    	ResultSet rest = null;
    	try {
    		log.info("sql is "+sql);
    		rest = this.executeQuery(sql.toString());
			while(rest.next()){
				Map map = new HashMap();
				map.put("ID", CommonTools.null2String(rest.getString("ID")));
				map.put("FUNCTIONID", CommonTools.null2String(rest.getString("FUNCTIONID")));
				map.put("NAME", CommonTools.null2String(rest.getString("NAME")));
				map.put("LANGUAGE", CommonTools.null2String(rest.getString("LANGUAGE")));
				map.put("FUNCTIONNAME", CommonTools.null2String(rest.getString("FUNCTIONNAME")));
				list.add(map);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			Dispose.dispose(rest);
		}
		return list;
    }
}
